package net.hn.hnms.biz.emgcy.service.impl;

import net.hn.hnms.biz.emgcy.domain.DutyWorkPo;
import net.hn.hnms.biz.emgcy.domain.EmgcyLocatePo;
import net.hn.hnms.biz.emgcy.domain.EmgcyPlanPo;
import net.hn.hnms.biz.emgcy.domain.dto.DutyWorkDto;
import net.hn.hnms.biz.emgcy.domain.vo.DutyWorkVo;
import net.hn.hnms.biz.emgcy.mapper.DutyWorkMapper;
import net.hn.hnms.biz.emgcy.service.DutyWorkService;
import net.hn.hnms.biz.emgcy.service.EmgcyLocateService;
import net.hn.hnms.biz.emgcy.service.EmgcyPlanService;
import net.hn.hnms.sys.common.core.domain.R;
import net.hn.hnms.sys.common.core.domain.ResponsePage;
import net.hn.hnms.sys.common.mybatis.core.page.PageQuery;
import net.hn.hnms.sys.file.api.RemoteFileService;
import net.hn.hnms.sys.file.api.domain.vo.SysOssVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;


/**
 * @Author: baijiajun
 * @Date: 2022/9/14
 */
@Service
public class DutyWorkServiceImpl implements DutyWorkService {
    @Autowired
    private DutyWorkMapper dutyWorkMapper;
    @Autowired
    private EmgcyPlanService emgcyPlanService;
    @Autowired
    private EmgcyLocateService emgcyLocateService;
    @Autowired
    private RemoteFileService remoteFileService;


    @Override
    public ResponsePage<DutyWorkVo> pageList(PageQuery pageQuery, DutyWorkDto dutyWorkDto) {
        pageQuery.setPageNum(pageQuery.getPageNum() >= 1 ? (pageQuery.getPageNum() - 1) * pageQuery.getPageSize():0);
        List<DutyWorkVo> list =  dutyWorkMapper.pageList(pageQuery.getPageSize(), pageQuery.getPageNum(),dutyWorkDto);
        if(list.isEmpty()){
            return new ResponsePage<>(new ArrayList(),0);
        }
        return new ResponsePage<>(list,list.get(0).getTotal());
    }

    @Override
    public Boolean add(DutyWorkPo dutyWorkPo) {
        int insert = dutyWorkMapper.insert(dutyWorkPo);
        if(insert>0){
            return true;
        }
        return false;
    }

    @Override
    public Boolean update(DutyWorkPo dutyWorkPo) {
        int update = dutyWorkMapper.updateById(dutyWorkPo);
        if(update>0){
            return true;
        }
        return false;
    }

    @Override
    public Boolean delete(Long id) {
        int delete = dutyWorkMapper.deleteById(id);
        if(delete>0){
            return true;
        }
        return false;
    }

    @Override
    public List plans(String eventType) {
        ArrayList<Object> list = new ArrayList<>();
        EmgcyPlanPo emgcyPlan = emgcyPlanService.getOne(eventType);
        EmgcyLocatePo emgcyLocate = emgcyLocateService.getOne(eventType);
        if(emgcyLocate!=null){
            R<List<SysOssVo>> listR = remoteFileService.listByIds(new Long[]{emgcyLocate.getInformationId().longValue()});
            List<SysOssVo> data = listR.getData();
            list.add(data.get(0));
        }
        if(emgcyLocate == null){
            list.add(null);
        }
        if(emgcyPlan!=null){
            R<List<SysOssVo>> listR = remoteFileService.listByIds(new Long[]{emgcyPlan.getInformationId().longValue()});
            SysOssVo sysOssVo = listR.getData().get(0);
            list.add(sysOssVo);
        }
        if(emgcyPlan == null){
            list.add(null);
        }
        return list;
    }
}
